package app

import (
	"net/http"
	"tapay-ws/pkg/errcode"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Ctx *gin.Context
}

type ResponseResult struct {
	List  interface{} `json:"list"`
	Pager Pager       `json:"pager"`
}

type Pager struct {
	// 页码
	Page int `json:"page"`
	// 每页数量
	PageSize int `json:"page_size"`
	// 总行数
	TotalRows int `json:"total_rows"`
}

type SummaryPager struct {
	OwnerCnt int `json:"owner_cnt"`
	//总交易量
	TotalVolume float32 `json:"total_volume"`
	//最低价
	FloorPrice float32 `json:"floor_price"`
}

func NewResponse(ctx *gin.Context) *Response {
	return &Response{
		Ctx: ctx,
	}
}

func (r *Response) ToResponse(result interface{}) {
	data := gin.H{}
	if m, ok := result.(gin.H); ok {
		data = m
	} else {
		if result != nil {
			data["data"] = result
		}
	}
	if _, ok := data["msg"]; !ok {
		data["msg"] = "ok"
	}
	data["code"] = http.StatusOK
	r.Ctx.JSON(http.StatusOK, data)
}

func (r *Response) ToResponseList(list interface{}, totalRows int) {
	r.Ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"list": list,
		"pager": Pager{
			Page:      GetPage(r.Ctx),
			PageSize:  GetPageSize(r.Ctx),
			TotalRows: totalRows,
		},
	})
}

func (r *Response) ToResponseListAndMap(list interface{}, totalRows int, m map[string]interface{}) {
	data := gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"list": list,
		"pager": Pager{
			Page:      GetPage(r.Ctx),
			PageSize:  GetPageSize(r.Ctx),
			TotalRows: totalRows,
		},
	}
	if m != nil {
		for k, v := range m {
			data[k] = v
		}
	}
	r.Ctx.JSON(http.StatusOK, data)
}

func (r *Response) ToErrorResponseError(err error) {
	errCode, ok := err.(*errcode.Error)
	if !ok {
		errCode = errcode.ErrorSystemFail.WithDetails(err.Error())
	}
	response := gin.H{"code": errCode.Code(), "msg": errCode.Msg()}
	details := errCode.Details()
	if len(details) > 0 {
		response["details"] = details
	}

	r.Ctx.JSON(errCode.StatusCode(), response)
}

func (r *Response) ToErrorResponse(err *errcode.Error) {
	response := gin.H{"code": err.Code(), "msg": err.Msg()}
	details := err.Details()
	if len(details) > 0 {
		response["details"] = details
	}

	r.Ctx.JSON(http.StatusOK, response)
}

// ------------------新的返回值-----------------
func (r *Response) SuccessResponse(data interface{}) {
	response := gin.H{
		"status":  http.StatusOK,
		"message": "success",
		"result":  data,
	}
	r.Ctx.JSON(http.StatusOK, response)
}

func (r *Response) ErrorResponse(err *errcode.Error) {
	response := gin.H{
		"status":  err.Code(),
		"message": err.Msg(),
		"result":  err.Details(),
	}
	r.Ctx.JSON(err.StatusCode(), response)
}

func (r *Response) SuccessResponseList(list interface{}, totalRows int) {
	result := &ResponseResult{
		List: list,
		Pager: Pager{
			Page:      GetPage(r.Ctx),
			PageSize:  GetPageSize(r.Ctx),
			TotalRows: totalRows,
		},
	}
	r.Ctx.JSON(http.StatusOK, gin.H{
		"status":  http.StatusOK,
		"message": "success",
		"result":  result,
	})
}

func (r *Response) ToResponseListNoPage(list interface{}) {
	r.Ctx.JSON(http.StatusOK, gin.H{
		"list": list,
	})
}

func (r *Response) ToResponseListByQueryTime(list interface{}, totalRows int, queryTime uint32) {
	r.Ctx.JSON(http.StatusOK, gin.H{
		"list": list,
		"pager": Pager{
			Page:      GetPage(r.Ctx),
			PageSize:  GetPageSize(r.Ctx),
			TotalRows: totalRows,
		},
		"query_time": queryTime,
	})
}
